import 'package:flutter/material.dart';
import 'package:flutter_lui/flutter_lui.dart';
import 'package:flutter_lui/src/l_style.dart';

enum LButtonShape {
  /// 圆角
  round,

  /// 圆形
  circle,

  /// 方形
  square,
}

// 按钮
class LButton extends StatelessWidget {
  /// 是否镂空按钮
  final bool? outline;

  /// 按钮类型 默认为主色
  final LUIType type;

  /// 按钮形状 默认为方型
  final LButtonShape shape;

  /// 按钮文本
  final String? text;

  /// 按钮文字颜色
  final Color? textColor;

  /// 按钮文字大小
  final double? textFontSize;

  /// 按钮背景色
  final Color? backgroundColor;

  /// 按钮要显示的图标
  final Widget? icon;

  /// 按钮显示内容 优先级最高
  final Widget? child;

  /// 按钮宽度
  final double? width;

  /// 按钮高度
  final double? height;

  /// 点击事件
  final void Function()? onTap;

  /// 是否禁用
  final bool? disabled;

  /// 圆角
  final double? radius;

  /// 是否有左上角圆角
  final bool? radiusTopLeft;

  /// 是否有右上角圆角
  final bool? radiusTopRight;

  /// 是否有左下角圆角
  final bool? radiusBottmLeft;

  /// 是否有右下角圆角
  final bool? radiusBottomRight;

  /// 外边距
  /// - [1]=>all
  /// - [1,1]=>[上下，右左]
  /// - [1,1,1]=> [上，右左，下]
  /// - [1,1,1,1]=> [上，右，下，左]
  final List<double> margin;

  /// 左外边距
  final double? marginLeft;

  /// 右外边距
  final double? marginRight;

  /// 上外边距
  final double? marginTop;

  /// 下外边距
  final double? marginBottom;

  /// 上下外边距
  final double? marginVertical;

  /// 左右外边距
  final double? marginHorizontal;

  const LButton({
    this.type = LUIType.primary,
    this.outline,
    this.shape = LButtonShape.square,
    this.text,
    this.backgroundColor,
    this.textFontSize,
    this.child,
    this.height,
    this.width,
    this.onTap,
    this.textColor,
    this.radius,
    this.icon,
    this.disabled = false,
    this.radiusBottmLeft = true,
    this.radiusBottomRight = true,
    this.radiusTopLeft = true,
    this.radiusTopRight = true,
    this.margin = const [0],
    this.marginLeft,
    this.marginRight,
    this.marginTop,
    this.marginBottom,
    this.marginVertical,
    this.marginHorizontal,
    key,
  })  : assert(!(child != null && text != null), 'child和text只能有一个'),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    Color? usetextColor = textColor;
    Color? useBackgroundColor = backgroundColor;
    Color? useBorderColor = Colors.transparent;
    Radius borderRadius = const Radius.circular(0);
    double? useWidth = width;
    double? useHeight = height ?? lrpx(36);
    Widget? childWidget = child;
    if (childWidget == null) {
      if (textColor == null) {
        if (outline == true) {
          if (type == LUIType.primary) {
            usetextColor = LUI.instance.primaryColor;
            useBorderColor = LUI.instance.primaryColor;
          } else if (type == LUIType.danger) {
            usetextColor = LUI.instance.dangerColor;
            useBorderColor = LUI.instance.dangerColor;
          } else if (type == LUIType.warning) {
            usetextColor = LUI.instance.warningColor;
            useBorderColor = LUI.instance.warningColor;
          } else if (type == LUIType.success) {
            usetextColor = LUI.instance.successColor;
            useBorderColor = LUI.instance.successColor;
          }
        } else {
          usetextColor = Colors.white;
        }
      }

      childWidget = LText(
        text!,
        fontSize: textFontSize ?? LStyle.fontSize,
        color: usetextColor,
      );
      if (icon != null) {
        childWidget = LBox(
          direction: BoxDirections.horizontal,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [icon!, Container(width: lrpx(5)), childWidget],
        );
      }
    }
    if (backgroundColor == null) {
      if (outline != true) {
        if (type == LUIType.primary) {
          useBackgroundColor = LUI.instance.primaryColor;
        } else if (type == LUIType.danger) {
          useBackgroundColor = LUI.instance.dangerColor;
        } else if (type == LUIType.warning) {
          useBackgroundColor = LUI.instance.warningColor;
        } else if (type == LUIType.success) {
          useBackgroundColor = LUI.instance.successColor;
        }
      } else {
        useBackgroundColor = Colors.transparent;
      }
    } else if (outline == true) {
      useBorderColor = backgroundColor!;
    }
    if (shape == LButtonShape.round) {
      borderRadius = Radius.circular(radius ?? LStyle.borderRadius);
    } else if (shape == LButtonShape.circle) {
      borderRadius = Radius.circular((width ?? 1000) / 2);
      useHeight = useWidth;
    }

    Widget btn = Material(
      color: Colors.transparent,
      child: InkWell(
        splashColor: useBorderColor.withOpacity(0.1),
        highlightColor: useBorderColor.withOpacity(0.1),
        onTap: disabled == true ? null : onTap,
        borderRadius: BorderRadius.only(
          topLeft: radiusTopLeft == true ? borderRadius : Radius.zero,
          topRight: radiusTopRight == true ? borderRadius : Radius.zero,
          bottomLeft: radiusBottmLeft == true ? borderRadius : Radius.zero,
          bottomRight: radiusBottomRight == true ? borderRadius : Radius.zero,
        ),
        child: Ink(
            decoration: BoxDecoration(
                color: outline == true
                    ? Colors.transparent
                    : useBackgroundColor!
                        .withOpacity(disabled == true ? 0.8 : 1),
                borderRadius: BorderRadius.only(
                  topLeft: radiusTopLeft == true ? borderRadius : Radius.zero,
                  topRight: radiusTopRight == true ? borderRadius : Radius.zero,
                  bottomLeft:
                      radiusBottmLeft == true ? borderRadius : Radius.zero,
                  bottomRight:
                      radiusBottomRight == true ? borderRadius : Radius.zero,
                ),
                border: Border.all(color: useBorderColor)),
            child: LBox(
              width: useWidth,
              height: useHeight,
              borderRadius: [borderRadius.x],
              padding: [0, lrpx(14)],
              child: Center(child: childWidget),
            )),
      ),
    );
    var marginValue = findTRBL(margin);
    var mt = marginTop ?? marginVertical ?? marginValue[0] ?? 0;
    var mr = marginRight ?? marginHorizontal ?? marginValue[1] ?? 0;
    var mb = marginBottom ?? marginVertical ?? marginValue[2] ?? 0;
    var ml = marginLeft ?? marginHorizontal ?? marginValue[3] ?? 0;
    if (mt != 0 || mr != 0 || mb != 0 || ml != 0) {
      btn = Padding(
          padding: EdgeInsets.only(top: mt, right: mr, bottom: mb, left: ml),
          child: btn);
    }
    return btn;
  }
}
